home *** CD-ROM | disk | FTP | other *** search
- /*******************************************************************************
- +
- + LEDA 2.1.1 11-15-1991
- +
- +
- + matrix.h
- +
- +
- + Copyright (c) 1991 by Max-Planck-Institut fuer Informatik
- + Im Stadtwald, 6600 Saarbruecken, FRG
- + All rights reserved.
- +
- *******************************************************************************/
-
-
-
-
- //------------------------------------------------------------------------------
- // matrices
- //
- // Stefan Naeher (1988)
- //------------------------------------------------------------------------------
-
- #ifndef MATRIXH
- #define MATRIXH
-
- #ifndef MATRIX_DEFAULT_DIM1
- #define MATRIX_DEFAULT_DIM1 2
- #endif
-
- #ifndef MATRIX_DEFAULT_DIM2
- #define MATRIX_DEFAULT_DIM2 2
- #endif
-
-
-
- #include <LEDA/basic.h>
- #include <LEDA/vector.h>
-
-
-
- struct matrix_rep {
-
- vector* v;
- int dim1;
- int dim2;
-
- matrix_rep(int, int);
- matrix_rep(const matrix_rep*);
- matrix_rep(int,int,double**);
-
- ~matrix_rep();
-
- OPERATOR_NEW(3)
- OPERATOR_DEL(3)
-
- };
-
-
-
- class matrix{
-
- matrix_rep* ptr;
-
- void check_dimensions(const matrix&) const;
-
- void flip_rows(int i,int j)
- { vector_rep* p = ptr->v[i].ptr;
- ptr->v[i].ptr = ptr->v[j].ptr;
- ptr->v[j].ptr = p;
- }
-
- double elem(int i, int j) const { return ptr->v[i].ptr->v[j]; }
-
- double** triang(const matrix&, int&) const;
-
- public:
-
-
- matrix() { ptr = new matrix_rep(MATRIX_DEFAULT_DIM1,MATRIX_DEFAULT_DIM2); }
-
- matrix(int d1, int d2) { ptr = new matrix_rep(d1,d2); }
- matrix(int d1, int d2, double** p) { ptr = new matrix_rep(d1,d2,p); }
- matrix(const matrix& mat) { ptr = new matrix_rep(mat.ptr); }
- matrix(void* p) { ptr = new matrix_rep((matrix_rep*) p ); }
-
- matrix(const vector&);
-
- void clear() { delete ptr; }
-
- ~matrix() { clear(); }
-
- int dim1() const { return ptr->dim1; }
- int dim2() const { return ptr->dim2; }
-
- vector& row(int) const;
- vector col(int i) const;
- matrix trans() const;
-
- matrix inv() const;
- double det() const;
-
- matrix solve(const matrix&) const;
- vector solve(const vector& b) const { return solve(matrix(b)); }
-
- operator vector() const;
-
- matrix& operator=(const matrix&);
-
- int operator==(const matrix&) const;
- int operator!=(const matrix& x) const { return !(*this == x); }
-
- vector& operator[](int i) const { return row(i); }
- double operator()(int, int) const;
- double& operator()(int, int);
-
- matrix operator+(const matrix&);
- matrix operator-(const matrix&);
-
- matrix operator*(double);
- matrix operator*(const matrix&);
- vector operator*(const vector& v) { return vector(*this * matrix(v)); }
-
- friend ostream& operator<<(ostream&, const matrix&);
- friend istream& operator>>(istream&, matrix&);
-
- friend void Print(matrix& M, ostream& out=cout) { out << M; }
- friend void Read(matrix& M, istream& in=cin) { in >> M; }
-
- friend void Clear(matrix& M) { M.clear(); }
- friend ent Copy(const matrix& M) { return new matrix_rep(M.ptr); }
- friend ent Init(matrix& M) { return new matrix_rep(M.ptr); }
- friend ent Ent(matrix& M) { return M.ptr; }
-
- friend int compare(matrix& x, matrix& y) { return int(x.ptr) - int(y.ptr); }
-
- };
-
-
- //------------------------------------------------------------------------------
- // MATRIX(cmp): vector with user defined linear order cmp
- //------------------------------------------------------------------------------
-
- #define MATRIX(cmp) name2(matrix_,cmp)
-
- #define MATRIXdeclare(cmp)\
- struct MATRIX(cmp) : public matrix \
- { MATRIX(cmp)(ent p) :(p) {}\
- MATRIX(cmp)(int d1, int d2) :(d1,d2) {}\
- MATRIX(cmp)(matrix m ) :(m) {}\
- MATRIX(cmp)(MATRIX(cmp)& m) :(m) {}\
- MATRIX(cmp)() {}\
- ~ MATRIX(cmp)() {}\
- };\
- \
- int compare(MATRIX(cmp)& x, MATRIX(cmp)& y) { return cmp(x,y); }
-
- #endif
-